package com.elbis.business.entity.authentication;

import static javax.persistence.GenerationType.IDENTITY;

import java.io.Serializable;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

import com.elbis.business.entity.DataBaseConstants;
import com.elbis.business.entity.Game;
import com.elbis.business.entity.Gamer;

@Entity
@Table(name = DataBaseConstants.TABLE_AUTHENTICATION, catalog = DataBaseConstants.CATALOG)
public class Authentication implements Serializable {

	private static final long serialVersionUID = -210053604471387930L;

	private Long id = null;

	@NotNull
	@Column(name = DataBaseConstants.COLUMN_GAMER_ID)
	private Gamer gamer = null;

	@NotNull
	@Column(name = DataBaseConstants.COLUMN_GAME_ID)
	private Game game = null;

	@NotNull
	@Size(max = 64)
	private String token = null;

	@NotNull
	private Date expires = null;

	@Id
	@GeneratedValue(strategy = IDENTITY)
	@Column(name = "id", nullable = false)
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	@ManyToOne(optional = false)
	@JoinColumn(name = DataBaseConstants.COLUMN_GAMER_ID)
	public Gamer getGamer() {
		return gamer;
	}

	public void setGamer(Gamer gamer) {
		this.gamer = gamer;
	}

	@OneToOne
	@JoinColumn(name = DataBaseConstants.COLUMN_GAME_ID)
	public Game getGame() {
		return game;
	}

	public void setGame(Game game) {
		this.game = game;
	}

	public String getToken() {
		return token;
	}

	public void setToken(String token) {
		this.token = token;
	}

	public Date getExpires() {
		return expires;
	}

	public void setExpires(Date expires) {
		this.expires = expires;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((game == null) ? 0 : game.hashCode());
		result = prime * result + ((gamer == null) ? 0 : gamer.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Authentication other = (Authentication) obj;
		if (game == null) {
			if (other.game != null)
				return false;
		} else if (!game.equals(other.game))
			return false;
		if (gamer == null) {
			if (other.gamer != null)
				return false;
		} else if (!gamer.equals(other.gamer))
			return false;
		return true;
	}

}
